<?php 

Class wechatController extends appController
{
	/**
	 * 微信公众平台与本系统连接的方法
	 */	
	public function weixin()
	{
		$wx = new weixinController();
		$data = $wx->data;
		
		/*验证*/
		if($_GET["echostr"])
		{
			$respon = $wx->valid(Model('wechat')->getToken());
			if($respon != '')
			{
				die($respon);
			}
		}

		$access_token = Model('wechat')->getAccessToken();
		
		/*自定义关键词回复*/
		if($data['Content'])
		{  
			$content = Model('wechat')->key_reply($data['Content']);
			if(!empty($content))
	 		{   
	   			if(!is_array($content))
	   			{   
	    			$wx->replyTextMsg($content);
	   			}
	   			else
	   			{
					$wx->replyImgMsg($content);
	   			}
	 		}
	 		else 
	 		{
	 			$areply = Model('wechat')->get_dreply();
	 			if(!is_array($areply))
	 			{
	 				$wx->replyTextMsg($areply);
	 			}
	 			else
	 			{
	 				$wx->replyImgMsg($areply);
	 			}
	 		}
		}
		
		/*自定义菜单回复*/
		if($data['EventKey'])
		{
			if(strstr($data['EventKey'], 'event_'))
			{
				$pos = strpos($data['EventKey'], 'event_');
				$key = intval(substr($data['EventKey'], $pos + 6));
				$result = Model('wechat')->get_msg($key);
				
				if(!is_array($result))
				{
					$wx->sendTextMsg($data['FromUserName'], $result);
				}
				else 
				{	
					if(!empty($result))
					{
					   	foreach($result as $k => $v)
					  	{
							$img_data[$k]['title'] = urlencode($v['title']);
							$img_data[$k]['description'] = urlencode($v['img_des']);
							$img_data[$k]['url'] = $v['url'];
							$img_data[$k]['picurl'] = 'http://'.$_SERVER['HTTP_HOST'].'/'.$v['thumb'];
					   	}	
						$wx->sendImgMsg($data['FromUserName'], $img_data);
					}
				}		
			}
		}
		
		/*关注时回复*/
		if($data['Event'] == 'subscribe')
		{
			$areply = Model('wechat')->get_areply();
			if(!is_array($areply))
			{
				$wx->replyTextMsg($areply);
			}
			else
			{
				$wx->replyImgMsg($areply);
			}
		}
		elseif($data['Event'] == 'unsubscribe')
		{
		
		}
		
		/*发送地址*/
// 		if($data['Location_X'] && $data['Location_Y'])
// 		{
// 			$img_one = array("url" => "http://".$_SERVER['HTTP_HOST']."/index.php?g=home&m=baidu&a=index&x=".$data['Location_X']."&y=".$data['Location_Y'], "thumb" => "Home/webroot/default/images/i96x66.jpg", "title" => "分店信息", "description" => "分店信息");
// 			$img_arr[] = $img_one;
// 			$wx->replyImgMsg($img_arr);
// 		}
	}
	
	public function index()
	{
		
	}
}